<template>
    <div>
        <div class="outmain ba_f heighthv pad_20 bor_rad5">
            <div class="shuju_title mar_b20">
                <div class="shuju_title_text">
                    <span>创建生日有礼活动</span>
                </div>
            </div>
            <div class="mar_t20 flex-bet flex-y-top">
                <div class="flex-g-1">
                    <el-form ref="form" :model="form" label-width="180px">
                        <el-form-item label="活动名称" :required='true' prop="name">
                            <el-input v-model="form.name" placeholder="请输入活动名称"></el-input>
                        </el-form-item>
                        <el-form-item label="活动时间" :required='true'>
                            <el-date-picker
                                    v-model="timeData"
                                    type="datetimerange"
                                    align="right"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    :default-time="['08:00:00', '23:59:59']">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="参与对象" :required='true'>
                            <el-radio-group v-model="form.people">
                                <el-radio label="1">所有人均可领取</el-radio>
                                <el-radio label="2">新用户</el-radio>
                                <el-radio label="3">会员可领</el-radio>
                                <el-radio label="4">指定标签用户可领</el-radio>
                            </el-radio-group>
                            <div class="mar_t10" v-show="form.people=='4'">
                                <el-select v-model="form.userLabel" clearable multiple filterable allow-create placeholder="请选择标签">
                                    <el-option  v-for="item in labelOptions" :key="item.id" :label="item.labelName" :value="item.id"></el-option>
                                </el-select>
                            </div>
                        </el-form-item>
                        <el-form-item label="优惠活动">
                            <div class="">
                                <div class="">
                                    <div class="mar_lr10">
                                        <el-checkbox-group v-model="form.isCoupon">
                                            <el-checkbox  true-label="1" false-label="2">赠送优惠券</el-checkbox>
                                        </el-checkbox-group>
                                    </div>
                                    <div v-show="form.isCoupon=='1'" class="flex">
                                        <el-select v-model="form.coupon" multiple filterable placeholder="请选择优惠券">
                                            <el-option v-for="item in comgrouping" :key="item.id" :label="item.name"
                                                       :value="item.id"></el-option>
                                        </el-select>
                                    </div>
                                </div>
                                <div class="t_l numberInput">
                                    <div class="mar_lr10">
                                        <el-checkbox-group v-model="form.isBalance">
                                            <el-checkbox  true-label="1" false-label="2">赠送余额</el-checkbox>
                                        </el-checkbox-group>
                                    </div>
                                    <div v-show="form.isBalance=='1'">
                                        <el-input-number v-model="form.balance" controls-position="right" :min="0.01" :max="99999999"
                                                         class=""></el-input-number>
                                        <span class="mar_l10">元</span>
                                    </div>
                                </div>
                                <div class="mar_t10 t_l numberInput">
                                    <div class="mar_lr10">
                                        <el-checkbox-group v-model="form.isIntegral">
                                            <el-checkbox  true-label="1" false-label="2">赠送积分</el-checkbox>
                                        </el-checkbox-group>
                                    </div>
                                    <div  v-show="form.isIntegral=='1'">
                                        <el-input-number v-model="form.integral" controls-position="right" :min="1" :max="99999999"
                                                         class=""></el-input-number>
                                        <span class="mar_l10">积分</span>
                                    </div>
                                </div>
<!--                                <div class="mar_t10 t_l numberInput">-->
<!--                                    <div class="mar_lr10">-->
<!--                                        <el-checkbox-group v-model="money3">-->
<!--                                            <el-checkbox  true-label="1" false-label="2">赠送成长值</el-checkbox>-->
<!--                                        </el-checkbox-group>-->
<!--                                    </div>-->
<!--                                    <div v-show="money3">-->
<!--                                        <el-input-number v-model="integral" controls-position="right" :min="1" :max="99999999"-->
<!--                                                         class=""></el-input-number>-->
<!--                                        <span class="mar_l10">成长值</span>-->
<!--                                    </div>-->
<!--                                </div>-->
                            </div>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="flex-g-0 pad_lr_20">
                    <div class="fon_16">常见问题</div>
                    <el-collapse v-model="collapseName" accordion class="mar_tb10" style="width: 260px">
                        <el-collapse-item title="什么是生日有礼活动？" name="1">
                            <div>针对平台用户生日当天的一个生日礼包活动。</div>
                        </el-collapse-item>
                        <el-collapse-item title="为什么要做生日有礼活动？" name="2">
                            <div>提高用户粘性，让用户在平台享受一定回馈福利</div>
                        </el-collapse-item>
                        <el-collapse-item title="哪些用户可以享受生日有礼活动" name="3">
                            <div>在个人资料完善了生日时间的用户，没有完善的则不享受此活动</div>
                        </el-collapse-item>
                    </el-collapse>
                    <div class="fon_16 mar_t20">活动规则</div>
                    <div class="color_9 mar_t10 lh30 fon_13">
                        <div class="">
                            <div>1.活动仅限完善了生日时间的用户，每个用户一年</div>
                            <div>内有且只能享受一次活动；</div>
                        </div>
                        <div>2.活动成本由平台承担，与店铺其他活动默认同享</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mar_t20 ba_f pad_20 t_c">
            <el-button type="primary" @click="submitForm">立即创建</el-button>
        </div>
        <attachment-img ref="img1" type="img" @attachment='imgadd1'></attachment-img>
    </div>
</template>
<script>
    import {getSaveBirthday,postSaveBirthday,getLabel} from "@/api/plug";
    import {getSelectCoupon} from "@/api/user";
    import {changeDate} from "@/utils/index";
    import attachmentImg from "@/plugins/attachment-img";


    export default {
        created() {
            if (this.$route.query.id) {
                this.id = this.$route.query.id
                this.edit()
            }
            this.getSelectCoupon()
            this.getLabel()
        },
        components: {attachmentImg,},
        data() {
            return {
                form: {
                    name: '',
                    people: '1',
                    isCoupon: '1',
                    isBalance: '1',
                    isIntegral: '1',
                    isGrowth: '1',
                    userLabel: [],
                    coupon: [],
                    balance:'',
                    integral:'',
                    growth:'',
                },
                comgrouping:[],
                labelOptions:[],
                timeData: [new Date(),new Date().setMonth(new Date().getMonth() + 3)],
                collapseName:'',
            }
        },
        methods: {
            async getSelectCoupon(){
                const {data} = await getSelectCoupon()
                this.comgrouping = data?data:[]
            },
            async getLabel(){
                const {data} = await getLabel()
                this.labelOptions = data?data:[]
            },
            async edit(){
                const {data} = await getSaveBirthday({id: this.id})
                this.form2 = this.form
                this.form = Object.assign(this.form2, data);
                if(data.startTime){
                    this.timeData = [data.startTime,data.endTime]
                }
            },
            enter(i=0){
                this.$refs['imgicon' + i].style.display = 'none'
            },
            leave(i=0){
                this.$refs['imgicon' + i].style.display = 'inline-block'
            },
            test(i = 0) {
                console.log(this.$refs, i)
                this.$refs['img' + i].dialogVisible = true
            },
            imgadd1(url) {
                this.form.icon = url.str
            },
            deleteMultiImage(item, index, type) {
                //typem没传是单图multigraph多图；index上传图片组件排序
                Array.prototype.remove = function (val) {
                    var index = this.indexOf(val);
                    if (index > -1) {
                        this.splice(index, 1);
                    }
                };
                if (type == 'multigraph') {
                    switch (index) {
                        case 2:
                            this.form2.environment.remove(item);
                            break
                    }
                } else {
                    switch (index) {
                        case 1:
                            this.form.icon = '';
                            break
                    }
                }
            },
            async submitForm() {
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        if(this.timeData){
                            this.form.startTime  = changeDate(this.timeData[0]);
                            this.form.endTime = changeDate(this.timeData[1]);
                        }
                        const {msg} = await postSaveBirthday(this.form);
                        this.$baseMessage(msg, "success");
                        this.$router.go(-1)
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
